package com.xuebank.nfe.nfecar.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PointF;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.licrafter.tagview.TagViewGroup;
import com.licrafter.tagview.views.ITagView;
import com.trello.rxlifecycle.android.FragmentEvent;
import com.wzn.libaray.utils.Logger;
import com.wzn.libaray.utils.cache.SharedPreferencesUtil;
import com.xuebank.nfe.nfecar.PicturePreviewActivity;
import com.xuebank.nfe.nfecar.PictureVideoPlayActivity;
import com.xuebank.nfe.nfecar.R;
import com.xuebank.nfe.nfecar.TagEditActivity;
import com.xuebank.nfe.nfecar.config.PictureConfig;
import com.xuebank.nfe.nfecar.config.PictureMimeType;
import com.xuebank.nfe.nfecar.entity.LocalMedia;
import com.xuebank.nfe.nfecar.globals.Constants;
import com.xuebank.nfe.nfecar.model.DeleteTagListSize;
import com.xuebank.nfe.nfecar.model.DeleteTagListSizeList;
import com.xuebank.nfe.nfecar.model.DeleteTagPosition;
import com.xuebank.nfe.nfecar.model.TagGroupModel;
import com.xuebank.nfe.nfecar.model.TagViewBodel;
import com.xuebank.nfe.nfecar.model.TagViewListModel;
import com.xuebank.nfe.nfecar.model.TouchEventXY;
import com.xuebank.nfe.nfecar.model.ViewPagerTagModel;
import com.xuebank.nfe.nfecar.rxbus2.RxBus;
import com.xuebank.nfe.nfecar.view.TagImageView;
import com.xuebank.nfe.nfecar.widget.longimage.ImageSource;
import com.xuebank.nfe.nfecar.widget.longimage.ImageViewState;
import com.xuebank.nfe.nfecar.widget.longimage.SubsamplingScaleImageView;


import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import rx.functions.Action1;

/**
 * @author：luck
 * @data：2018/1/27 下午7:50
 * @描述:图片预览
 */

public class SimpleFragmentAdapter extends PagerAdapter  {
    private TagViewListModel images = new TagViewListModel();
    private Context mContext;
    private OnCallBackActivity onBackPressed;
    private int deleteTagProtion;
   private DeleteTagListSize mDeleteTagListSize;
    private DeleteTagListSizeList mDeleteTagListSizeList = new DeleteTagListSizeList();
    private TagImageView mTagImageView;
    private View mCurrentView;
    int i = 0;
    int deleteSize = 0;




    public interface OnCallBackActivity {
        /**
         * 关闭预览Activity
         */
        void onActivityBackPressed();
    }

    public SimpleFragmentAdapter(TagViewListModel images, Context context,
                                 OnCallBackActivity onBackPressed) {
        super();
        this.images = images;
        this.mContext = context;
        this.onBackPressed = onBackPressed;
    }

    @Override
    public int getCount() {
        if (images != null) {
            return images.getTagViewBodel().size();
        }
        return 0;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        (container).removeView((View) object);

    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {



        com.wzn.libaray.event.RxBus.getDefault().toObserverable(DeleteTagPosition.class)
                .subscribe(new Action1<DeleteTagPosition>() {
                    @Override
                    public void call(DeleteTagPosition deleteTagPosition) {
                        deleteTagProtion=  deleteTagPosition.getPosition();

                    }
                });

        com.wzn.libaray.event.RxBus.getDefault().toObserverable(String.class)
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        if (s == "laile") {
                            if (mDeleteTagListSizeList.getMDeleteTagListSize().size() != 0) {
                                com.wzn.libaray.event.RxBus.getDefault().post(mDeleteTagListSizeList);
                            }

                        }
                    }
                });



        com.wzn.libaray.event.RxBus.getDefault().toObserverable(TagViewListModel.class)
                .subscribe(new Action1<TagViewListModel>() {
                    @Override
                    public void call(TagViewListModel tagViewListModel) {
                        images = tagViewListModel;
                    }
                });

        final View contentView = LayoutInflater.from(container.getContext())
                .inflate(R.layout.picture_image_preview, container, false);





        // 常规图控件
        final TagImageView imageView = (TagImageView) contentView.findViewById(R.id.preview_image);
        final ImageView imageView1 = (ImageView)contentView.findViewById(R.id.iv_playss);

        mTagImageView = imageView;
        imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext,"我是第"+position+"个我被点到了",Toast.LENGTH_SHORT).show();
            }
        });



        if (images.getTagViewBodel().get(position) != null) {

            final String path;


            path=images.getTagViewBodel().get(position).getPath();
                RequestOptions options = new RequestOptions()
                        .diskCacheStrategy(DiskCacheStrategy.ALL);
                Glide.with(contentView.getContext())
                        .asBitmap()
                        .load(path)
                        .apply(options)
                        .into(new SimpleTarget<Bitmap>(480, 800) {
                            @Override
                            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                                imageView.setBitmap(resource);
//                                imageView1.setImageBitmap(resource);
                            }
                        });
//            s = new ViewPagerTagModel();

            imageView.setEditMode(true);
            imageView.setTagGroupScrollListener(new TagViewGroup.OnTagGroupDragListener() {
                @Override
                public void onDrag(@NotNull final TagViewGroup container, float percentX, float percentY) {
                    deleteSize = 0;
                    imageView.onDrag(container, percentX, percentY);
                    imageView.showDeleteImage();

                    final ArrayList<Float> mSize = new ArrayList<>();
                    com.wzn.libaray.event.RxBus.getDefault().toObserverable(TouchEventXY.class)
                            .subscribe(new Action1<TouchEventXY>() {
                                @Override
                                public void call(TouchEventXY s) {
                                    s.getX();
                                }
                            });




//                    Logger.d("position是哪个","position是哪个"+position);
                    com.wzn.libaray.event.RxBus.getDefault().toObserverable(String.class)
                            .subscribe(new Action1<String>() {
                                @Override
                                public void call(String s) {
//                                    container.getTagList()
                                    if (s == "deleveTag") {
                                        if (images.getTagViewBodel().get(position).getTabText().size() != 0) {
                                            if (deleteSize == 0) {
                                                int mTagPosition = 0;
                                                for ( int i=0;i<images.getTagViewBodel().get(position).getTabText().size();i++) {
                                                    if (deleteTagProtion == images.getTagViewBodel().get(position).getTabText().get(i).getTags().get(0).getId()) {
                                                        mDeleteTagListSize = new DeleteTagListSize();
                                                        mTagPosition = i;
                                                        images.getTagViewBodel().get(position).getTabText().remove(mTagPosition);
                                                        deleteSize = 1;
                                                        mDeleteTagListSize.setImagePosition(position);
                                                        mDeleteTagListSize.setTagPosition(i);
                                                        mDeleteTagListSizeList.getMDeleteTagListSize().add(mDeleteTagListSize);
                                                        new AlertDialog.Builder(mContext)
                                                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                                                    @Override
                                                                    public void onClick(DialogInterface dialog, int which) {
                                                                        dialog.dismiss();
                                                                    }
                                                                })
                                                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                                    @Override
                                                                    public void onClick(DialogInterface dialog, int which) {
                                                                        imageView.removeTagGroup(container);
                                                                        dialog.dismiss();
                                                                    }
                                                                }).setTitle("删除标签").setMessage("你确定要删除该标签吗？")
                                                                .create().show();
                                                    }

                                                }
                                            }


                                            imageView.hintDeleteImage();
                                        }


                                    } else if (s=="NodeleveTag") {
                                        imageView.hintDeleteImage();
                                    }
                                }
                            });
                }
            });
            imageView.setTagGroupClickListener(new TagViewGroup.OnTagGroupClickListener() {
                @Override
                public void onCircleClick(@NotNull TagViewGroup container) {
                    Toast.makeText(mContext, "点击中心圆", Toast.LENGTH_SHORT).show();

                }

                @Override
                public void onTagClick(@NotNull TagViewGroup container, @NotNull ITagView tag, final int position) {
                    imageView.onTagClicked(container, tag, position);
                    com.wzn.libaray.event.RxBus.getDefault().toObserverable(String.class)
                            .subscribe(new Action1<String>() {
                                @Override
                                public void call(String s) {
                                    if (s == "heihei") {
                                        Logger.d("position是哪个","position是哪个"+position);
                                    }
                                }
                            });
                }

                @Override
                public void onLongPress(@NotNull final TagViewGroup container) {

                }
            });
            if (images.getTagViewBodel().get(position).getTabText().size()!=0) {

                for (int i=0;i<images.getTagViewBodel().get(position).getTabText().size();i++) {

                    TagGroupModel  model = new TagGroupModel();
                    model=images.getTagViewBodel().get(position).getTabText().get(i);

                    imageView.addTagGroup(model);
                }

            }



        }

        (container).addView(contentView, 0);


        return contentView;
    }
}
